<template>
  <div class="mvlist">
    <div class="mvlist-item" v-for="item in mvlist" :key="item.id">
      <router-link :to="{ name: 'watchMv', params: { id: item.id } }">
        <img :src="item.cover" :alt="item.name" class="" />
        <span class="playcount">
          <img src="../assets/eye.png" alt="" />
         <span class="count">{{ item.playCount }}</span> 
        </span>
        <p class="item-name">{{ item.name }}</p>
        <p class="item-artistName">{{ item.artistName }}</p>
      </router-link>
    </div>
    <Button v-on:click="getmore">加载更多</Button>
  </div>
</template>

<script>
export default {
  props: ["keywords"],
  data() {
    return {
      mvlist: [],
      offset: 0,
      mvcount: -1,
    };
  },
  created() {
    if (this.keywords != "") {
      this.offset = 0;
      this.getMvList();
    } else {
      this.getAllMv();
    }
  },
  methods: {
    getAllMv() {
      this.$http
        .get("/mv/all?&offset=" + this.offset)
        .then((response) => {
          if (response.data.code == 200) {
            this.mvcount = -1;
            this.mvlist = this.mvlist.concat(response.data.data);
            this.offset = this.offset + 30;
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },

    getMvList() {
      this.$http
        .get(
          "/search?keywords=" +
            this.keywords +
            "&type=1004&offset=" +
            this.offset
        )
        .then((response) => {
          if (response.data.code == 200) {
            this.mvcount = response.data.result.mvCount;
            this.mvlist = this.mvlist.concat(response.data.result.mvs);
            this.offset = this.offset + 30;
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    getmore() {
      console.log(this.mvcount)
      if (this.mvcount > 0) {
        this.getMvList();
      } else if (this.mvcount === -1) {
        this.getAllMv();
        console.log('11')
      } else {
        console.log("error");
      }
    },
  },
};
</script>

<style lang="scss">
.mvlist {
  display: flex;
  flex-wrap: wrap;
  padding-left: 10px;
  .mvlist-item {
    width: calc(calc(100% / 2) - 10px);
    margin: 10px 10px 0 0;
    background: #fff;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    border-radius: 10px;
    padding: 10px;
    position: relative;
  }
  img {
    width: 100%;
    border-radius: 10px;
    
  }
  .item-name {
    font-size: 14px;
    color: #333;
    margin: 10px 0;
  }
  .playcount {
    justify-content: space-around;
    color: #fff;
    position: absolute;
    right: 20px;
    top: 20px;
    font-size: 12px;
    img {
      width: 20px;
      height: 20px;
    }
    .count{
      position: relative;
      bottom:5px;
      font-weight: bold;
    }
  }
  .item-name {
    font-size: 20px;
    font-weight: bold;
    text-align: left;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .item-artistName {
    font-size: 10px;
    font-weight: bold;
    text-align: left;
    margin-top: 5px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  Button {
    width: 90%;
    margin: 10px 5%;
    border-radius: 20px;
  }
}
</style>